# 验证敏感文件是否被 .gitignore 正确排除
# PowerShell 脚本

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  敏感文件保护验证脚本" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# 检查 .gitignore 文件是否存在
if (-not (Test-Path ".gitignore")) {
    Write-Host "❌ 错误: .gitignore 文件不存在！" -ForegroundColor Red
    exit 1
}
Write-Host "✅ .gitignore 文件存在" -ForegroundColor Green

# 检查 data/.gitignore 文件是否存在
if (-not (Test-Path "data/.gitignore")) {
    Write-Host "❌ 错误: data/.gitignore 文件不存在！" -ForegroundColor Red
    exit 1
}
Write-Host "✅ data/.gitignore 文件存在" -ForegroundColor Green

Write-Host ""
Write-Host "检查敏感文件..." -ForegroundColor Yellow

# 需要被忽略的文件列表
$sensitiveFiles = @(
    "data/users.json",
    "data/sign_logs.json",
    "config/config.yaml"
)

# 检查 Git 是否初始化
if (-not (Test-Path ".git")) {
    Write-Host ""
    Write-Host "⚠️  Git 仓库未初始化" -ForegroundColor Yellow
    Write-Host "   只能检查 .gitignore 配置，无法验证 Git 行为" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "请运行以下命令初始化 Git 仓库：" -ForegroundColor Cyan
    Write-Host "   git init" -ForegroundColor White
    Write-Host ""
    
    # 检查 .gitignore 配置
    Write-Host "检查 .gitignore 配置..." -ForegroundColor Yellow
    $gitignoreContent = Get-Content ".gitignore" -Raw
    $dataGitignoreContent = Get-Content "data/.gitignore" -Raw
    
    $allOk = $true
    foreach ($file in $sensitiveFiles) {
        if ($file -eq "config/config.yaml") {
            if ($gitignoreContent -match "config/config\.yaml") {
                Write-Host "  ✅ $file - 已配置忽略规则" -ForegroundColor Green
            } else {
                Write-Host "  ❌ $file - 缺少忽略规则！" -ForegroundColor Red
                $allOk = $false
            }
        } elseif ($file -like "data/*.json") {
            if ($dataGitignoreContent -match "\*\.json") {
                Write-Host "  ✅ $file - 已配置忽略规则" -ForegroundColor Green
            } else {
                Write-Host "  ❌ $file - 缺少忽略规则！" -ForegroundColor Red
                $allOk = $false
            }
        }
    }
    
    if ($allOk) {
        Write-Host ""
        Write-Host "✅ .gitignore 配置正确！" -ForegroundColor Green
    } else {
        Write-Host ""
        Write-Host "❌ .gitignore 配置有问题，请检查！" -ForegroundColor Red
    }
    
    exit 0
}

Write-Host "✅ Git 仓库已初始化" -ForegroundColor Green
Write-Host ""

# 使用 git check-ignore 验证
Write-Host "验证文件是否被 Git 忽略..." -ForegroundColor Yellow
Write-Host ""

$allIgnored = $true

foreach ($file in $sensitiveFiles) {
    if (Test-Path $file) {
        $result = git check-ignore -v $file 2>&1
        if ($LASTEXITCODE -eq 0) {
            Write-Host "  ✅ $file" -ForegroundColor Green
            Write-Host "     规则: $result" -ForegroundColor Gray
        } else {
            Write-Host "  ❌ $file - 未被忽略！" -ForegroundColor Red
            $allIgnored = $false
        }
    } else {
        Write-Host "  ⚪ $file - 文件不存在（正常）" -ForegroundColor Gray
    }
}

Write-Host ""

# 检查是否有文件已被 Git 追踪
Write-Host "检查敏感文件是否已被 Git 追踪..." -ForegroundColor Yellow
Write-Host ""

$tracked = $false
foreach ($file in $sensitiveFiles) {
    $result = git ls-files $file 2>&1
    if ($result) {
        Write-Host "  ⚠️  $file - 已被 Git 追踪！" -ForegroundColor Red
        Write-Host "     请运行: git rm --cached $file" -ForegroundColor Yellow
        $tracked = $true
    }
}

if (-not $tracked) {
    Write-Host "  ✅ 没有敏感文件被追踪" -ForegroundColor Green
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan

if ($allIgnored -and -not $tracked) {
    Write-Host "  ✅ 所有检查通过！可以安全上传" -ForegroundColor Green
} else {
    Write-Host "  ⚠️  发现问题，请修复后再上传" -ForegroundColor Yellow
}

Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# 显示将被提交的文件预览（如果有暂存的文件）
$stagedFiles = git diff --cached --name-only 2>&1
if ($stagedFiles) {
    Write-Host "当前暂存的文件（将被提交）：" -ForegroundColor Cyan
    $stagedFiles | ForEach-Object { Write-Host "  - $_" -ForegroundColor White }
    Write-Host ""
}

Write-Host "💡 提示：" -ForegroundColor Yellow
Write-Host "  - 上传前请确保已删除或清空敏感数据文件" -ForegroundColor White
Write-Host "  - 使用 'git status --ignored' 查看所有被忽略的文件" -ForegroundColor White
Write-Host "  - 使用 'git ls-files' 查看将被提交的文件列表" -ForegroundColor White
Write-Host ""

